package com.example.xianji.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

/**
 * 将图片保存到内存中
 *
 * @author me
 */
public class SaveSdk {
    /**
     * 将图片下载到SD卡缓存起来。
     *
     * @param imageUrl 图片的URL地址。
     */
    public static void downloadImage(String imageUrl, String name,
                                     Context context) {

        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            Log.d("TAG", "monted sdcard");
        } else {
            Log.d("TAG", "has no sdcard");
        }

        HttpURLConnection con = null;
        FileOutputStream fos = null;
        BufferedOutputStream bos = null;
        BufferedInputStream bis = null;
        File imageFile = null;
        try {
            URL url = new URL(imageUrl);

            con = (HttpURLConnection) url.openConnection();
            con.setConnectTimeout(5 * 1000);
            con.setReadTimeout(15 * 1000);
            con.setDoInput(true);
            con.setDoOutput(true);

            bis = new BufferedInputStream(con.getInputStream());

            imageFile = new File(getImagePath(imageUrl, name, context));

            /**
             * 写入文件中
             */

            fos = new FileOutputStream(imageFile);

            bos = new BufferedOutputStream(fos);
            byte[] b = new byte[1024];
            int length;
            while ((length = bis.read(b)) != -1) {
                bos.write(b, 0, length);
                bos.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bis != null) {
                    bis.close();
                }
                if (bos != null) {
                    bos.close();
                }
                if (con != null) {
                    con.disconnect();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    /**
     * 获取图片的本地存储路径。
     *
     * @param imageUrl 图片的URL地址。
     * @return 图片的本地存储路径。
     */
    public static String getImagePath(String imageUrl, String name,
                                      Context context) {
        Log.i("zzzzz", imageUrl + "");
        int lastSlashIndex = imageUrl.lastIndexOf("/");

        String imageName = imageUrl.substring(lastSlashIndex + 1);
        String imageDir = context.getCacheDir() + name;
        File file = new File(imageDir);
        if (!file.exists()) {
            file.mkdirs();

            /*
             * try { file.createNewFile(); } catch (IOException e) { //
             * Auto-generated catch block e.printStackTrace(); }
             */
        }
        String imagePath = imageDir + imageName;
        return imagePath;
    }

    /**
     * 删除存在本地的图片和指定文件夹
     */

    public static void deleteFile_baike(Context context, String name) {
        String imageDir = name;

        File dirFile = new File(imageDir);
        if (!dirFile.exists()) {
            return;
        }
        if (dirFile.isDirectory()) {
            String[] children = dirFile.list();
            for (int i = 0; i < children.length; i++) {
                Log.i("ddawwwww", children[i]);
                new File(dirFile, children[i]).delete();
            }
        }

        dirFile.delete();
    }
}
